%% copy riak-0.14.2 source to learn dynamo implementation and erlang
%% author: lichuang
-module(edynamo_sup).

-behaviour(supervisor).
%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
-define (IF (Bool, A, B), if Bool -> A; true -> B end).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
  supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================
init([]) ->
  VMaster = {edynamo_vnode_master,
    {edynamo_vnode_master, start_link,
      [edynamo_kv_vnode, edynamo_kv_legacy_vnode]},
    permanent, 5000, worker,
    [edynamo_vnode_master]},
  EdynamoPb = [{edynamo_pb_socket_sup, {edynamo_pb_socket_sup, start_link, []},
      permanent, infinity, supervisor,
      [edynamo_pb_socket_sup]},
      {edynamo_pb_listener,
      {edynamo_pb_listener, start_link, []},
      permanent, 5000,
      worker,
      [edynamo_pb_listener]}
  ],  
  Children = lists:flatten(
    [?CHILD(edynamo_vnode_sup, supervisor),
     ?CHILD(edynamo_ring_events, worker),
     ?CHILD(edynamo_ring_manager, worker),
     ?CHILD(edynamo_node_watcher, worker),
     ?CHILD(edynamo_node_watcher_events, worker),
     VMaster,
     EdynamoPb
    ]),

  {ok, {{one_for_one, 10, 10}, Children}}.
